package com.example.auth.controller;

import com.example.auth.common.R;
import com.example.auth.model.dto.LoginDTO;
import com.example.auth.model.dto.UserInfoDTO;
import com.example.auth.model.entity.SysRole;
import com.example.auth.service.SysRoleService;
import com.example.auth.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 认证授权控制器
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private SysUserService userService;
    
    @Resource
    private SysRoleService roleService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody @Valid LoginDTO loginDTO) {
        System.out.println("收到登录请求: " + loginDTO);
        return userService.login(loginDTO);
    }

    /**
     * 用户注销
     */
    @PostMapping("/logout")
    public R<String> logout() {
        userService.logout();
        return R.ok("注销成功");
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public R<UserInfoDTO> getUserInfo() {
        return userService.getCurrentUserInfo();
    }

    /**
     * 获取用户角色
     */
    @GetMapping("/roles/{userId}")
    public String[] getUserRoles(@PathVariable Long userId) {
        return userService.getUserRoles(userId);
    }
    
    /**
     * 获取系统所有角色
     */
    @GetMapping("/allRoles")
    public R<List<SysRole>> getAllRoles() {
        List<SysRole> roles = roleService.list();
        return R.ok(roles);
    }
} 